package cn.study.zjan.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;

import cn.study.zjan.MBaseActivity;
import cn.study.zjan.R;
import cn.study.zjan.common.lib.viewioc.ContentViewById;
import cn.study.zjan.common.lib.viewioc.ViewById;
import cn.study.zjan.widget.ClipRevealFrame;

/**
 * 圆覆盖动画
 */
@ContentViewById(R.layout.activity_clip_radius)
public class ClipRadiusActivity extends MBaseActivity {

    @ViewById(R.id.rlayout)
    ClipRevealFrame mClipRevealLayout;

    private boolean isEnd = true;//动画是否结束,默认为结束状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_clip_radius, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 按钮点击事件
     *
     * @param view
     */
    public void clipRadiusClick(View view) {
        //判断动画状态
        if (isEnd) {
            isEnd = false;
            //计算中心点
            int cx = (view.getLeft() + view.getRight()) / 2;
            int cy = (view.getTop() + view.getBottom()) / 2;
            //计算圆起始半径
            float startRadius = 1f * view.getWidth() / 2f;
            //计算圆结束半径
            float endRadius = (float) Math.hypot(
                    cx - mClipRevealLayout.getX(),
                    cy - mClipRevealLayout.getY());


            boolean select = view.isSelected();
            Log.d(TAG, "cx:" + cx + ",cy:" + cy + ",startRadius:" + startRadius + ",endRadius:" + endRadius);
            //是否显示
            if (select) {
                hideClipReveal(cx, cy, startRadius, endRadius);
            } else {
                showClipReveal(cx, cy, startRadius, endRadius);
            }

            view.setSelected(!select);
        }
    }

    /**
     * 显示布局
     *
     * @param cx
     * @param cy
     * @param startRadius
     * @param endRadius
     */
    private void showClipReveal(int cx, int cy, float startRadius, float endRadius) {

        mClipRevealLayout.setVisibility(View.VISIBLE);

        Animator animator = createCircularReveal(mClipRevealLayout, cx, cy, startRadius, endRadius);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setDuration(500);
        animator.start();
    }

    /**
     * 隐藏布局
     *
     * @param cx
     * @param cy
     * @param startRadius
     * @param endRadius
     */
    private void hideClipReveal(int cx, int cy, float startRadius, float endRadius) {

        Animator animator = createCircularReveal(mClipRevealLayout, cx, cy, endRadius, startRadius);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mClipRevealLayout.setVisibility(View.INVISIBLE);
            }
        });
        animator.setDuration(500);
        animator.start();
    }


    /**
     * 创建圆覆盖动画
     *
     * @param view
     * @param x
     * @param y
     * @param startRadius
     * @param endRadius
     * @return
     */
    private Animator createCircularReveal(final ClipRevealFrame view, int x, int y, float startRadius, float endRadius) {
        final Animator reveal;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            reveal = ViewAnimationUtils.createCircularReveal(view, x, y, startRadius, endRadius);
        } else {
            view.setClipOutLines(true);
            view.setClipCenter(x, y);
            reveal = ObjectAnimator.ofFloat(view, "ClipRadius", startRadius, endRadius);
            reveal.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation) {
                    view.setClipOutLines(false);
                }
            });
        }
        reveal.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                isEnd = true;
            }
        });

        return reveal;
    }

}
